/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ComExWeb.util;

import ComExWeb.ui.converters.ConverterStorageBean;
import java.util.List;
import java.util.Map;
import javax.el.ValueExpression;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.context.Flash;
import org.primefaces.model.DualListModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author 
 */
public class JsfUtil {

    public static FacesContext getFC() {

        return FacesContext.getCurrentInstance();

    }

    public static ExternalContext getEC() {

        return FacesContext.getCurrentInstance().getExternalContext();

    }

    public static Map getSessionMap() {

        return getEC().getSessionMap();

    }

    /**
     * <p>Return a <code>Map</code> of the session scope attributes for the
     * current user's session.  Note that calling this method will cause a
     * session to be created if there is not already one associated with
     * this request.</p>
     */
    public static Map getSession() {

        return getEC().getSessionMap();

    }

    public static Object getSessionVal(String key) {
        return getSession().get(key);
    }

    public static Object setSessionVal(String key, Object val) {
        return getSession().put(key, val);
    }

    public static Flash getFlash() {
        return getEC().getFlash();
    }

    public static Object getFlashVal(String key) {
        return getFlash().get(key);
    }

    public static void setFlashVal(String key, Object val) {
        getFlash().put(key, val);
    }

    public static String getAppPath() {
        return getEC().getRequestContextPath();
    }
    private static final Logger log = LoggerFactory.getLogger(JsfUtil.class);

    public static void configPickList(List allObjects, List selectedObjects, DualListModel model) {
        log.debug(">>>>>>>> entrou em configPickList()");
        model.setSource(allObjects);
        model.setTarget(selectedObjects);
        model.getSource().removeAll(selectedObjects);
    }

    public static <T> T getJsfBean(Class<T> c, String beanName) {
        ValueExpression vex =
                getFC().getApplication().getExpressionFactory().createValueExpression(getFC().getELContext(), beanName, c);
        return (T) vex.getValue(getFC().getELContext());
    }

    // --------------------------------------------------------- Message Methods
    /**
     * <p>Enqueue a global <code>FacesMessage</code> (not associated
     * with any particular component) containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_INFO</code>.
     * </p>
     *
     * @param summary Summary text for this message
     */
    public static void info(String summary) {

        getFC().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO, summary, null));

    }

    /**
     * <p>Enqueue a <code>FacesMessage</code> associated with the
     * specified component, containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_INFO</code>.
     * </p>
     *
     * @param component Component with which this message is associated
     * @param summary Summary text for this message
     */
    public static void info(UIComponent component, String summary) {

        getFC().addMessage(component.getClientId(getFC()),
                new FacesMessage(FacesMessage.SEVERITY_INFO, summary, null));

    }

    /**
     * <p>Enqueue a global <code>FacesMessage</code> (not associated
     * with any particular componen) containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_WARN</code>.
     * </p>
     *
     * @param summary Summary text for this message
     */
    public static void warn(String summary) {

        getFC().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_WARN, summary, null));

    }

    /**
     * <p>Enqueue a <code>FacesMessage</code> associated with the
     * specified component, containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_WARN</code>.
     * </p>
     *
     * @param component Component with which this message is associated
     * @param summary Summary text for this message
     */
    public static void warn(UIComponent component, String summary) {

        getFC().addMessage(component.getClientId(getFC()),
                new FacesMessage(FacesMessage.SEVERITY_WARN, summary, null));

    }

    /**
     * <p>Enqueue a global <code>FacesMessage</code> (not associated
     * with any particular componen) containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_ERROR</code>.
     * </p>
     *
     * @param summary Summary text for this message
     */
    public static void error(String summary) {

        getFC().addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, null));


    }

    public static void messageDialogInformacao(String summary,String mensagem) {

        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,summary, mensagem );
        getFC().addMessage(null, msg);

    }
    /**
     * <p>Enqueue a <code>FacesMessage</code> associated with the
     * specified component, containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_ERROR</code>.
     * </p>
     *
     * @param component Component with which this message is associated
     * @param summary Summary text for this message
     */
    public static void error(UIComponent component, String summary) {

        getFC().addMessage(component.getClientId(getFC()),
                new FacesMessage(FacesMessage.SEVERITY_ERROR, summary, null));

    }

    /**
     * <p>Enqueue a global <code>FacesMessage</code> (not associated
     * with any particular componen) containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_FATAL</code>.
     * </p>
     *
     * @param summary Summary text for this message
     */
    public static void fatal(String summary) {

        getFC().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_FATAL, summary, null));

    }

    /**
     * <p>Enqueue a <code>FacesMessage</code> associated with the
     * specified component, containing the specified summary text
     * and a message severity level of <code>FacesMessage.SEVERITY_FATAL</code>.
     * </p>
     *
     * @param component Component with which this message is associated
     * @param summary Summary text for this message
     */
    public static void fatal(UIComponent component, String summary) {

        getFC().addMessage(component.getClientId(getFC()),
                new FacesMessage(FacesMessage.SEVERITY_FATAL, summary, null));

    }
}
